//------------------------------------------------------------------------------
// <copyright file="TavernsCollectionControl.xaml.cs" company="Damian Antonowicz">
//       copyright © 2010 Damian Antonowicz
// </copyright>
// <author>Damian Antonowicz</author>
// <email>poczta@damianantonowicz.pl</email>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using DotBeer.Business.Entities;
using DAnton.Utilities.Extensions;
using DotBeer.Business.Components;

namespace DotBeer.UI.WPFClient
{
    /// <summary>
    /// Interaction logic for WindowTavernsCollection.xaml
    /// </summary>
    public partial class TavernsCollectionControl : UserControl, IOptionsControl
    {
        #region private members

        TavernsCollection tavernsColl;
        IList<Tavern> tavernsCopy;

        #endregion

        #region constructors

        public TavernsCollectionControl()
        {
            InitializeComponent();

            try
            {
                this.tavernsColl = TavernsCollectionComponent.Load();
                this.DataContext = this.tavernsColl.Taverns;
                this.tavernsCopy = this.tavernsColl.Taverns.Clone();
            }
            catch (Exception ex)
            {
                CustomMessageBox.Show(String.Format("Error occured while loading taverns collection: \n{0}", ex.Message), "Error");
            }
        }

        #endregion

        #region public methods

        #region IOptionsControl Members

        public bool Save()
        {
            try
            {
                TavernsCollectionComponent.Save(this.tavernsColl);
            }
            catch(Exception ex)
            {
                CustomMessageBox.Show(String.Format("Error occured while saving taverns collection: \n{0}", ex.Message), "Error");
                return false;
            }

            return true;
        }

        public void Cancel()
        {
            this.tavernsColl.Taverns.Clear();
            this.tavernsColl.Taverns.AddRange(this.tavernsCopy);
        }

        #endregion

        #endregion

        #region private methods

        private void ButtonAdd_Click(object sender, RoutedEventArgs e)
        {
            WindowAddNewObject winNew = new WindowAddNewObject("tavern");

            if (winNew.ShowDialog() == true)
            {
                string newTavernName = winNew.NewObjectName.Trim();

                if (this.tavernsColl.Taverns.SingleOrDefault(tavern => tavern.Name.ToUpper() == newTavernName.ToUpper()) == null)
                {
                    Tavern tavern = new Tavern()
                    {
                        Name = newTavernName
                    };

                    this.tavernsColl.Taverns.Add(tavern);
                }
                else
                    CustomMessageBox.Show(String.Format("Tavern: \"{0}\" already is on the list.", newTavernName),
                                        "Error");
            }
        }

        private void ListBoxTaverns_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete && this.ListBoxTaverns.SelectedItem != null)
                ((ObservableCollection<Tavern>)this.ListBoxTaverns.ItemsSource).Remove((Tavern)this.ListBoxTaverns.SelectedItem);
        }

        private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            e.Handled = !NumericValidation.IsNumeric(e.Text);
        }

        #endregion
    }
}
